<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    // 手写reduce
    function reduceFinal (callback, initial) {
      if (typeof callback !== "function"){
        throw new Error('TYPE ERROR ====')
      }

      if (typeof initial === "undefined") {
        initial = this[0]
        for (let i = 1; i < this.length; i++) {
          initial = callback(initial, this[i], i, this)
        }
      } else {
        for (let i = 0; i < this.length; i++) {
          console.log('🚀 ~ This is a result of console.log ~ ✨: ', initial);
          initial = callback(initial, this[i], i, this)
        }
      }
        return initial
    }

    Array.prototype.reduceFinal = reduceFinal
    const result2 = [1, 2, 3, 4, 5].reduceFinal(function (pre, cur, index, arr) {
      // console.log('🚀 ~ This is a result of console.log ~ ✨: ', pre);
      return pre + cur
    }, 12)
    console.log('🚀 ~ This is a result of console.log ~ ✨: ', result2);

  </script>
</body>
</html>
